package com.oocl.im.handler.impl;

import com.oocl.im.action.impl.SynAction;
import com.oocl.im.constants.ServerConstants;
import com.oocl.im.constants.ServerMap;
import com.oocl.im.entity.Protocol;
import com.oocl.im.outputstream.impl.NetOutput;
import com.oocl.im.entity.Client;
import com.oocl.im.handler.IHandle;

import java.io.ObjectOutputStream;
import java.util.*;

public class LoginHandle implements IHandle{

	ObjectOutputStream oos ;

	public LoginHandle(ObjectOutputStream os) {
		this.oos = os;
	}

	@Override
	public void handle(Client client, Protocol protocol) {
		System.out.println("here is in login handle");
		System.out.println("login handle client is "+client);

		ServerMap.clientMap.put(client.getAccount(), client);
		System.out.println("ServerMap.socketMap is :"+ServerMap.socketMap);
		ServerMap.socketMap.put(client, oos);
		Map<String, Object> params = new HashMap<>();
		params.put(ServerConstants.STATUS, ServerConstants.SUCCESS);
		params.put(ServerConstants.TO, client);
		List<String> list = new ArrayList<String>(ServerMap.clientMap.keySet());
		params.put(ServerConstants.LIST,list);
		Protocol res = new Protocol(ServerConstants.RESPONSE, params);

		System.out.println("login handle protocol: "+res);
		System.out.println("ServerMap.clientMap.size() is :"+ServerMap.clientMap.size());
		Iterator iterator = ServerMap.clientMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			System.out.println("login handle entry:  "+entry);
		}

		try {
			SynAction handle=new SynAction();
			handle.execute();
			NetOutput tmpOut = new NetOutput(oos);
			tmpOut.write(res);

		} catch (Exception e) {
			e.printStackTrace();
		}
		//id




		
	}

	

}
